package com.beans;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class PasswordValidator implements Validator {
   public void validate(FacesContext context, 
                        UIComponent component, 
                        Object obj)
           throws ValidatorException {
       String password = (String) obj;
       
       if(password.length() < 6) {
           FacesMessage message = new FacesMessage(
                FacesMessage.SEVERITY_ERROR, 
                "the length less than 6", 
                "the length cant less than 6");
           throw new ValidatorException(message);
       }
       
       if(!password.matches(".+[0-9]+")) {
           FacesMessage message = new FacesMessage(
                  FacesMessage.SEVERITY_ERROR, 
                  "the secret must ", 
                  "Password must be composed of characters or numbers");
           throw new ValidatorException(message);
       }
   }
}